package com.crayon.cardbiz.entry;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.crayon.cardbiz.R;
import com.crayon.cardbiz.entry.item.FragmentChildItemPhotos;
import com.crayon.cardbiz.fragment.FragmentRoot;
import com.crayon.util.GDB;

abstract public class FragmentChildPager extends FragmentRoot implements
OnPageChangeListener
,OnClickListener{

	abstract public int onSelectIndex();
	static final int NUM_ITEMS = 4;
	MyAdapter mAdapter;
	ViewPager mPager;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view  = inflater.inflate(R.layout.fragment_pager, container, false);
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mAdapter = new MyAdapter(getChildFragmentManager());
		mPager = (ViewPager)getView().findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		// Watch for button clicks.
//		UI.bindOnClickEvent(getView(), R.id.goto_first, this);
//		UI.bindOnClickEvent(getView(), R.id.goto_last, this);
		
		getActivity().runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mPager.setCurrentItem(onSelectIndex(), true);
				Fragment fragment = mAdapter.getItem(onSelectIndex());
				fragment.onResume();
			}
		});
		
		mPager.setOnPageChangeListener(this);
	}


	@Override
	public String getTopTitle() {
		// TODO Auto-generated method stub
		return "앨범종류";
	}

	public  class MyAdapter extends FragmentPagerAdapter {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return NUM_ITEMS;
		}

		@Override
		public Fragment getItem(int position) {
			return FragmentChildItemPhotos.newInstance(position);
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		int position = mPager.getCurrentItem();
		Fragment fragment = mAdapter.getItem(position);
		fragment.onResume();
		
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		if(mAdapter!=null ){
			GDB.log(getClass(), ">>> call onDestroy....");
			for(int i=0;i <mAdapter.getCount();i++){
				try {
					Fragment fragment = mAdapter.getItem(i);
					mPager.getAdapter().destroyItem(mPager, i, fragment);
					getChildFragmentManager().beginTransaction().remove(fragment);
				} catch (Exception e) {
					// TODO: handle exception
					GDB.log(getClass(), "err clear " + e.getMessage());
				}
			}
		}
		super.onDestroy();
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.goto_first:
		case R.id.goto_last:

//			Intent intent = new Intent(getActivity(),ActivitySelectImage.class);
//			startActivity(intent);
//			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//	        ft.replace(R.id.simple_fragment, newFragment);
//	        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//	        ft.addToBackStack(null);
//	        ft.commit();
			break;
		}
	}
	
	
}

